-- @owner:yangjian
-- @date:2024-4-7
-- @testpoint:1.去除大对象使用限制，测试lo_lseek函数使用和参数的表现，合理报错

--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_function_case0009_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_function_case0009_1();

--环境准备
drop table if exists t_opengauss_large_object_function_case0009_1 cascade;
CREATE TABLE t_opengauss_large_object_function_case0009_1 (loid oid,fd int);
INSERT INTO t_opengauss_large_object_function_case0009_1 (loid) SELECT lo_create(100);

--step1:测试fd参数为存在值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,0);
end;
/

--step2:测试fd参数为不存在值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(1,5,0);
end;
/
end;
--step3:测试fd参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek('a',5,0);
end;
/
end;
--step4:测试offset参数为负数;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,-1,0);
end;
/
end;
--step5:测试offset参数超过int4;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,2147483648,0);
end;
/
end;
--step6:测试offset参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,'a',0);
end;
/
end;
--step7:测试offset参数为null类型;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,null,0);
end;
/

--step8:测试whence参数为正常值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,0);
end;
/
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,1);
end;
/
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,2);
end;
/

--step9:测试whence参数为不符合要求的int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,4);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,-1);
end;
/
end;
--step10:测试whence参数超过int4;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,2147483648);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,-2147483649);
end;
/
end;
--step11:测试whence参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,'a');
end;
/
end;
--step12:测试whence参数null;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,5,null);
end;
/

--step13:测试2g以上使用;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(0,4294967296,0);
end;
/
end;
--清理环境
select lo_unlink(loid) from t_opengauss_large_object_function_case0009_1;
select f_opengauss_large_object_function_case0009_1();
drop table if exists t_opengauss_large_object_function_case0009_1 cascade;
drop function f_opengauss_large_object_function_case0009_1();
